﻿using Microsoft.EntityFrameworkCore;
using System.Data;
using Zws.Dapper.Interfaces;

namespace Zws.Dapper.Implements
{
    public class UnitOfWork : IUnitOfWork
    {
        private readonly DbContext _dbContext;
        private IDbTransaction transaction;
        public UnitOfWork(DbContext dbContext)
        {
            this._dbContext = dbContext;
        }
        public IDbTransaction GetTransAction()
        {
            return transaction;
        }
        public void BeginTransAction()
        {
            var conn = this._dbContext.Database.GetDbConnection();
            conn.Open();
            transaction = conn.BeginTransaction();
        }

        public void Commit()
        {
            transaction.Commit();
            this._dbContext.SaveChanges();
        }

        public void RollBack()
        {
            transaction.Rollback();
            //this._dbContext.SaveChanges();
        }
    }
}
